package com.wyw.leetcode.learning.simple;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Title   leetcode topic 104
 * @Description 二叉树的最大深度
 *      二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
 * @Author Mr Wu yewen.wu.china@gmail.com
 * Update History:
 * Author        Time            Content
 */
public class Topic104 {

    public static void main(String[] args) {
        TreeNode node = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(3);
        TreeNode node4 = new TreeNode(4);
        TreeNode node5 = new TreeNode(5);
        TreeNode node6 = new TreeNode(6);
        TreeNode node7 = new TreeNode(7);
        TreeNode node8 = new TreeNode(8);
        TreeNode node9 = new TreeNode(9);
        TreeNode node10 = new TreeNode(10);

        node.left = node2;
        node2.left = node4;
        node4.right = node8;
        node2.right = node5;
        node10.right = node3;
        node3.left = node6;
        node5.right = node9;
        node3.right = node7;
        node7.left = node10;

        System.out.println("end" + maxDepth(node));
    }

    /**
     * 广度优先
     * @param root
     * @return
     */
    public static int maxDepth(TreeNode root) {
        int res = 0;
        if (root == null) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            int currentLevelSize = queue.size();
            res += 1;
            for (int i = 1; i <= currentLevelSize; ++i) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
        }

        return res;
    }

    /**
     * 深度优先
     * @param root
     * @return
     */
    public static int maxDepth1(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int leftHeight = maxDepth(root.left);
            int rightHeight = maxDepth(root.right);
            return Math.max(leftHeight, rightHeight) + 1;
        }

    }

}
